<?php
namespace App\Service\CommonDataService;
use App\Service\BaseService;
use App\Model\PssModel\ExpressSupplier;
use App\Model\PssModel\ExpressSupplierCode;
use App\Service\Code\Normal;
use Hyperf\DbConnection\Db;
use App\Model\PssModel\Express;
/**
 * 获取上下游物流服务对接配置
 * @author chenyun
 */
class ExpressSupplierDataService extends BaseService
{
    /**
     * 获取三方服务配置列表
     * @param string userType
     * @param int $limit
     * @param int $page
     */
    public function queryExpressSupplierList(string $userType, int $limit, int $page)
    {
    
        $limit      = $limit ?? 15;
        $page       = $page ?? 0;
        $where      = [];
        $fields = [
            'id as ID',
            'user_type as userType',
            'user_name as userName',
            'express_supplier_name as expressSupplierName',
            'express_name as expressName',
            'express_code as expressCode',
        ];

        if($userType){
            $where[]=["user_type",$userType];
        }
        $pageInfo = ExpressSupplier::instance()
        ->where($where)
        ->orderBy('ID','desc')
        ->paginate($limit,$fields, 'page', $page,true)
        ->toArray();
        $list = [
            'count' => $pageInfo['total'],
            'data'  => $pageInfo['data'],
        ];
        return Normal::returnCode(Normal::SUC,"获取成功",$list);
    }
    /**
     * 获取基础物流表
     */
    public function queryExpressList()
    {
        $list = ExpressSupplierCode::instance()
        ->select([
            Db::raw('id as ID'),
            Db::raw('user_type as userType'),
            Db::raw('user_name as userName'),
        ])->get();
        return Normal::returnCode(Normal::SUC,"获取成功",$list);
    }
    
    /**
     * 新增三方物流信息
     * @param string $userType
     * @param string $expressSupplierName
     * @param string $expressName
     * @param string $expressCode
     */
    public function addExpressSupplier(string $userType,string $expressSupplierName,string $expressName,string $expressCode)
    {
        if( empty($userType) ) {
            return Normal::returnCode(Normal::FAIL, "供应商业务编码不能为空");
        }
        if( empty($expressSupplierName) ) {
            return Normal::returnCode(Normal::FAIL, "上游供应商物流快递名称不能为空");
        }
        if( empty($expressName) ) {
            return Normal::returnCode(Normal::FAIL, "下游电商平台物流公司名称不能为空");
        }
        if( empty($expressCode) ) {
            return Normal::returnCode(Normal::FAIL, "下游电商平台物流快递编码不能为空");
        }

        $expressSupplierCode = ExpressSupplierCode::instance()->where("user_type",$userType)->get()->first();
        if(empty($expressSupplierCode)){
            return Normal::returnCode(Normal::FAIL, "供应商业务编码不存在，请核实！");
        }
        $data = [
            'user_type'                 => $userType,
            'user_name'                 => $expressSupplierCode["user_name"],
            'express_supplier_name'     => $expressSupplierName,
            'express_name'              => $expressName,
            'express_code'              => $expressCode,
        ];
        $list = ExpressSupplier::instance()->where($data)->first();
        if($list){
            return Normal::returnCode(Normal::FAIL, "该数据已存在，不能重复添加！");
        }
        
        //如果是云仓物流映射，添加、修改前判断大云仓是否有该物流映射，没有，提醒添加者去查看云仓码表
        if($userType == "sys_yc"){
            $express_where["name"] = $expressName;
            $express_where["code"] = $expressCode;
            $res = Express::instance()->where($express_where)->first();
            if(!$res){
                return Normal::returnCode(Normal::FAIL, "【云仓物流码表{$expressName}-{$expressCode}不对，请去https://www.yuque.com/shupi-zzj5p/oskksk/lv5wcb中查询】");
            }
        }
        
        $data["express_name"] = $expressName;
        $data["express_code"] = $expressCode;
        $data["cd"] = time();
        $res  =  ExpressSupplier::instance()->add($data);
        if($res){
            return Normal::returnCode(Normal::SUC, "添加成功！");
        }else{
            return Normal::returnCode(Normal::FAIL, "添加失败！");
        }
    }
    
    /**
     * 修改三方物流信息
     * @param int $ID
     * @param string $userType
     * @param string $expressSupplierName
     * @param string $expressName
     * @param string $expressCode
     */
    public function updateExpressSupplier(int $ID,string $userType,string $expressSupplierName,string $expressName,string $expressCode)
    {
        if( empty($ID) ) {
            return Normal::returnCode(Normal::FAIL, "ID不能为空");
        }
        if( empty($userType) ) {
            return Normal::returnCode(Normal::FAIL, "供应商业务编码不能为空");
        }
        if( empty($expressSupplierName) ) {
            return Normal::returnCode(Normal::FAIL, "上游供应商物流快递名称不能为空");
        }
        if( empty($expressName) ) {
            return Normal::returnCode(Normal::FAIL, "下游电商平台物流公司名称不能为空");
        }
        if( empty($expressCode) ) {
            return Normal::returnCode(Normal::FAIL, "下游电商平台物流快递编码不能为空");
        }
        $list = ExpressSupplier::instance()->where("id","ID")->first();
        if($list){
            return Normal::returnCode(Normal::FAIL, "该数据不存在，请核实！");
        }

        $expressSupplierCode = ExpressSupplierCode::instance()->where("user_type",$userType)->get()->first();
        if(empty($expressSupplierCode)){
            return Normal::returnCode(Normal::FAIL, "供应商业务编码不存在，请核实！");
        }
        $user_name = $expressSupplierCode["user_name"];
        $data = [
            'user_type'                 => $userType,
            'user_name'                 => $user_name,
            'express_supplier_name'     => $expressSupplierName,
            'express_name'              => $expressName,
            'express_code'              => $expressCode,
        ];
        $list = ExpressSupplier::instance()->where($data)->first();
        if($list && $ID !=$list["id"]){
            return Normal::returnCode(Normal::FAIL, "该数据【{$userType}-{$user_name}-{$expressSupplierName}】已存在，请核实！");
        }

        //如果是云仓物流映射，添加、修改前判断大云仓是否有该物流映射，没有，提醒添加者去查看云仓码表
        if($userType == "sys_yc"){
            $express_where["name"] = $expressName;
            $express_where["code"] = $expressCode;
            $res = Express::instance()->where($express_where)->first();
            if(!$res){
                return Normal::returnCode(Normal::FAIL, "【云仓物流码表{$expressName}-{$expressCode}不对，请去https://www.yuque.com/shupi-zzj5p/oskksk/lv5wcb中查询】");
            }
        }
        
        $data["express_name"]           = $expressName;
        $data["express_code"]           = $expressCode;
        $data["cd"]                     = time();
        $res  =  ExpressSupplier::instance()->where("id",$ID)->update($data);
        if($res){
            return Normal::returnCode(Normal::SUC, "修改成功！");
        }else{
            return Normal::returnCode(Normal::FAIL, "修改失败！");
        }
    }
    /**
     * 获取基础物流表
     */
    public function querySupplierExpress(string $userType,array $expressSupplierName)
    {
        $list = ExpressSupplier::instance()->where('user_type',$userType)->whereIn('express_supplier_name',$expressSupplierName)->get();
        return Normal::returnCode(Normal::SUC,"获取成功",$list);
    }
}